package com.googlecode.protoserv;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;

public class JettyRpcServer {

  public static JettyRpcServer serve(int port, String path, RpcManager rpcman)
      throws Exception {
    JettyRpcServer server = new JettyRpcServer(8080);
    server.register("/", rpcman);
    server.start();
    return server;
  }

  private Server webServer;

  public JettyRpcServer(int port) {
    this.webServer = new Server(port);
  }

  public Server getWebServer() {
    return this.webServer;
  }

  public void register(String path, RpcManager rpcman) {
    Context context = new Context(this.webServer, path);
    context.setAttribute(RpcServlet.getRpcManageKeyForPath(path), rpcman);
    context.addServlet(RpcServlet.class, "/*");
  }

  public void start() throws Exception {
    this.webServer.start();
  }
}
